//
//  ZMYSettingCollectionViewController.m
//  奇阅
//
//  Created by 奇阅科技 on 14-10-27.
//  Copyright (c) 2014年 奇阅科技. All rights reserved.
//

#import "ZMYSettingCollectionViewController.h"
#import "ZMYSettingHeadView.h"
#import "ZMYSettingCollectionViewCell.h"
#import "ZMYHadPayFavoritesTableViewController.h"
#import "Config.h"
#import "BookShopingRootViewController.h"
#import "BookrackCollectionViewController.h"
#import "QYLoadingView.h"
#import "GuidePageView.h"
@interface ZMYSettingCollectionViewController ()<ZMYSettingCollectionViewCellDelegate>
{
    ZMYSettingHeadView *headView;
    
}
@property (nonatomic,strong)QYLoadingView *loadingView;
@end

@implementation ZMYSettingCollectionViewController

-(void)dealloc{
    NSLog(@"我的界面释放了");
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setBarBackgroundImage:@"navBackground"];
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
//    headView = nil;
//    [self.collectionView reloadData];
    [self getAdditional];
}
-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    if (![[NSUserDefaults standardUserDefaults] boolForKey:@"userInfoGuide"]) {
        GuidePageView *guideView = [[GuidePageView alloc]initWithGuideImage:[UIImage imageNamed:@"userInfoGuide"]];
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"userInfoGuide"];
        [guideView showView];
    }
}
-(void)getAdditional{
    UserAccount *user = [UserAccount currentAccount];
    if (user) {
        [[ZMYNetWorkAPI default] additionalResponse:^(NSDictionary *resultDic) {
            UserAccount *user = [UserAccount currentAccount];
            user.hadBuyNum =[resultDic objectForKey:@"count_book"];
            user.bookReviewNum =[resultDic objectForKey:@"count_comments"];
            user.collectionNum =[resultDic objectForKey:@"count_myfavor"];
            [UserAccount saveAccount:user];
//            headView = nil;
            [self.collectionView reloadData];
        } error:^(NSString *errorStr) {
        }];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

#pragma mark <UICollectionViewDataSource>

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 1;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    ZMYSettingCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ZMYSettingCollectionViewCell" forIndexPath:indexPath];
    [cell initDataArray];
    cell.delegate = self;
    return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(self.view.bounds.size.width, self.view.bounds.size.height-300);
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    return CGSizeMake(self.view.bounds.size.width, 300);
}

-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    if ([kind isEqualToString: UICollectionElementKindSectionHeader]) {
        if (headView == nil) {
            headView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"ZMYSettingHeadView" forIndexPath:indexPath];
            [headView stretchHeaderForTableView:collectionView withView:headView];
            __weak typeof(self) weakSelf = self;
            [headView refreshPosition:^{
                if (!weakSelf.loadingView) {
                    weakSelf.loadingView = [[[NSBundle mainBundle]loadNibNamed:@"QYLoadingView" owner:self options:nil] lastObject];
                }
                [weakSelf.loadingView showView];
                [[ZMYNetWorkAPI default]getNewUserInfoResponse:^(NSDictionary *resultDic) {
                    [weakSelf.loadingView closeView];
                    [headView initInfoLabel];
                } error:^(NSString *errorStr) {
                    [weakSelf.loadingView closeView];
                    [AppDelegate showHUDAndHide:errorStr view:weakSelf.view];
                }];
            }];
        }
        [headView initInfoLabel];
        return headView;
    }
    return nil;
}
#pragma mark <UICollectionViewDelegate>
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{

}
/*
// Uncomment this method to specify if the specified item should be highlighted during tracking
- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath {
	return YES;
}
*/

/*
// Uncomment this method to specify if the specified item should be selected
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
*/

/*
// Uncomment these methods to specify if an action menu should be displayed for the specified item, and react to actions performed on the item
- (BOOL)collectionView:(UICollectionView *)collectionView shouldShowMenuForItemAtIndexPath:(NSIndexPath *)indexPath {
	return NO;
}

- (BOOL)collectionView:(UICollectionView *)collectionView canPerformAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
	return NO;
}

- (void)collectionView:(UICollectionView *)collectionView performAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
	
}
*/
- (IBAction)loginButtonClick:(id)sender {
    [self presentViewController:[[UIStoryboard storyboardWithName:@"BookRack" bundle:nil] instantiateViewControllerWithIdentifier:@"NavLoginViewController"] animated:YES completion:nil];
}
- (IBAction)toBookRack:(id)sender {
    if ([self frostedBookrackViewController]) {
        [self.navigationController popToViewController:[self frostedBookrackViewController] animated:YES];
    }else if ([self frostedViewController]) {
        [self.navigationController popToRootViewControllerAnimated:YES];
        [[self frostedViewController]hideSideViewController:YES];
    }else{
        [[AppDelegate delegate]goToBookRack];
    }
}
- (IBAction)toBookShopping:(id)sender {
    if ([self frostedBookShoppingViewController]) {
        [self.navigationController popToViewController:[self frostedBookShoppingViewController] animated:YES];
    }else{
        BookShopingRootViewController *shoppingView = [[UIStoryboard storyboardWithName:@"BookShop" bundle:Nil]instantiateViewControllerWithIdentifier:@"BookShopingRootViewController"];
        [self.navigationController pushViewController:shoppingView animated:YES];
    }
}
- (ZMYRootViewController *)frostedViewController
{
    UIViewController *iter = self.parentViewController;
    while (iter) {
        if ([iter isKindOfClass:[ZMYRootViewController class]]) {
            return (ZMYRootViewController *)iter;
        } else if (iter.parentViewController && iter.parentViewController != iter) {
            iter = iter.parentViewController;
        } else {
            iter = nil;
        }
    }
    return nil;
}
- (BookrackCollectionViewController *)frostedBookrackViewController
{
    for (UIViewController *iter in self.navigationController.viewControllers) {
        if ([iter isKindOfClass:[BookrackCollectionViewController class]]) {
            return (BookrackCollectionViewController *)iter;
        }
    }
    return nil;
}
- (BookShopingRootViewController *)frostedBookShoppingViewController
{
    for (UIViewController *iter in self.navigationController.viewControllers) {
        if ([iter isKindOfClass:[BookShopingRootViewController class]]) {
            return (BookShopingRootViewController *)iter;
        }
    }
    return nil;
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    [headView scrollViewDidEndDragging:scrollView];
}
- (void)scrollViewDidScroll:(UIScrollView*)scrollView{
    if (scrollView.contentOffset.y>0) {
        scrollView.contentOffset = CGPointMake(scrollView.contentOffset.x, 0);
        return;
    }
    [headView scrollViewDidScroll:scrollView];
}
- (void)viewDidLayoutSubviews
{
    [headView resizeView];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"fromHadPay"]) {
        ZMYHadPayFavoritesTableViewController *viewController = (ZMYHadPayFavoritesTableViewController *)[segue destinationViewController];
        viewController.isHadPay = YES;
    }else if ([segue.identifier isEqualToString:@"fromFavorites"]) {
        ZMYHadPayFavoritesTableViewController *viewController = (ZMYHadPayFavoritesTableViewController *)[segue destinationViewController];
        viewController.isHadPay = NO;
    }
}
-(void)pushToViewController:(UIViewController *)viewController{
    [self.navigationController pushViewController:viewController animated:YES];
}
@end
